﻿using System;
using System.Collections.Generic;
using System.Text;
using T.ServiceComponent.Error;

namespace T.FAS.Runtime.Base.Common
{
    public static class ExceptionHelper
    {
        /// <summary>
        /// 获取异常上下文中的目标地址
        /// </summary>
        /// <param name="e">异常信息</param>
        /// <returns>目标地址</returns>
        public static string GetTargetFromContext(Exception e)
        {
            if (e == null || e.Data == null)
                return string.Empty;
            if (e.Data.Contains("ServerIP"))
                return Convert.ToString(e.Data["ServerIP"]);
            else
                return GetTargetFromContext(e.InnerException);
        }

        public static Exception GetLastError(Exception e)
        {
            if (e.InnerException == null || string.IsNullOrEmpty(e.InnerException.Source))
                return e;
            else
                return GetLastError(e.InnerException);
        }

        public static string GetErrorLevel(Exception e)
        {
            if (e is TException ex)
            {
                return Convert.ToString(ex.Level);
            }

            return string.Empty;
        }
        /// <summary>
        /// 是否远程异常
        /// </summary>
        /// <param name="e">异常对象</param>
        /// <returns>true：远程异常，false：本地异常</returns>
        public static bool IsRemoteException(Exception e)
        {
            if (e == null || e.Data == null)
                return false;
            if (!e.Data.Contains("IsRemoteException"))
                return false;
            else
                return true;
        }
    }
}
